package FenxiaoService

import (
	"context"
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/entity"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
)

// HandleOrderByOrderId 根据订单ID 处理分销订单，一般由支付完成事件触发
func HandleOrderByOrderId(orderId int) {
	ctx := context.Background()
	var foList []*entity.FenxiaoOrder
	err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "fenxiao_order").Where(
		"handle_status=0 AND order_id=?", orderId).Scan(&foList)
	if err != nil {
		return
	}

	if foList == nil {
		return
	}
	err = g.DB().Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		for _, item := range foList {
			err := HandleOrderByFenxiaoOrder(ctx, tx, item)
			if err != nil {
				return err
			}
		}
		return nil
	})
	if err != nil {
		return
	}
}
